
package com.arm.service;

import com.arm.module.Order;
import com.arm.module.OrderItem;
import com.arm.module.Payment;
import com.arm.module.User;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by baggio on 2017/1/1.
 */
public class PayBase {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProductEntityService productEntityService;

    /**
     * 微信支付通知状态： 成功 SUCCESS
     */
    protected static final String WECHAT_PAY_NOTTIFY_STATUS_SUCCESS = "SUCCESS";
    /**
     * 微信支付应答状态：成功 OK
     */
    protected static final String WECHAT_PAY_NOTTIFY_RETURN_MSG_OK = "OK";
    /**
     * 微信支付状态：失败 FAIL
     */
    protected static final String WECHAT_PAY_NOTTIFY_STATUS_FAIL = "FAIL";


    protected void payin(Long orderId, String acqSeqId) {
        logger.info("【payIN===》》》】");
        Order order = orderService.getOrderById(orderId);
        logger.info("【payIN===》》》order】" + order.toString());
        List<OrderItem> orderItemList = order.getOrderItemList();
        order.setStatus(OrderService.PAYED);

        User user = userService.getUserById(orderId);
        for (OrderItem orderItem : orderItemList) {
            if (orderItem.getStatus() == OrderService.CANCEL) {
                logger.info("项目已经去取消，跳出循环。。。。。");
                continue;
            }
            orderItem.setStatus(OrderService.WAIT_DISPATCH);
            orderItem.setCreateTime(new Date());
            orderItemService.modifyOrderItem(orderItem);
            if (orderItem.getStatus() == OrderService.COMPELTEED) {
                orderItemService.completeOrderItem(orderItem.getOrderItemId());
            }
            logger.info("支付成功，较少数量");
        }


        Payment payment = paymentService.getPaymentByOrderId(orderId);
        if (payment != null) {
            payment.setStatus(OrderService.PAYED);
            payment.setCreateTime(new Date());
            payment.setExtOrderId(acqSeqId);
            paymentService.modifyPayment(payment);
        }

        orderService.modifyOrder(order);
    }


    protected String getProductName(String orderId) {

        Order order = orderService.getOrderById(Long.parseLong(orderId));
        if (order == null) {
            throw new MyRunTimeExcption("订单不存在，支付失败！");
        }
        //商户订单号，商户网站订单系统中唯一订单号，必填


        //订单名称，必填
        StringBuffer productName = new StringBuffer();
        List<OrderItem> orderItemList = order.getOrderItemList();

        for (OrderItem orderItem : orderItemList) {
            productName.append(orderItem.getProductEntity().getName());

        }
        return productName.toString();
    }


}
